package com.tbit.main.controller;

import com.tbit.common.constant.OperationPlatform;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.Result;
import com.tbit.main.service.OperaPswService;
import com.tbit.manager.api.GiftCardUserApi;
import com.tbit.manager.api.dto.GiftCardUserDTO;
import com.tbit.manager.api.vo.UserGiftCardAreaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 区域礼品卡用户余额
 */
@RestController
@RequestMapping("/giftCardUser")
@Validated
public class GiftCardUserController {
    @Autowired
    private GiftCardUserApi giftCardUserApi;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 查询用户区域礼品卡余额
     *
     * @param accountId 用户区域id
     * @param userId    用户id
     * @return GiftCardUserDTO
     */
    @GetMapping("/getByUser")
    public Result<GiftCardUserDTO> getByUser(@NotNull Integer accountId, @NotNull Integer userId) {
        GiftCardUserDTO giftCardUserDTO = giftCardUserApi.getGiftCard(accountId, userId);
        return Result.success(giftCardUserDTO);
    }

    /**
     * 礼品卡余额充值
     */
    @GetMapping("/rechargeGiftMoney")
    public Result<String> rechargeGiftMoney(@NotNull Integer accountId, @NotNull Integer userId, @NotNull Integer money,
                                            String remark, String operaPsw) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        // 金额不能等于0
        if (money == 0) {
            throw new BaseException("money.not.zero");
        }
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.USER_OPERA_UPDATE_MONEY.equals(operaPsw)) {
            operaPswService.checkOperaPsw(LoginUserContextHolder.getLoginUser(), OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(),
                    operaPsw);
        }

        GiftCardUserDTO giftCardUserDTO = new GiftCardUserDTO();
        giftCardUserDTO.setUserId(userId);
        giftCardUserDTO.setMoney(money);
        giftCardUserDTO.setRemark(remark);
        giftCardUserDTO.setLoginUserAccountUserId(loginUser.getAccountUserId());
        giftCardUserDTO.setLoginUserType(loginUser.getAccountUserType());
        giftCardUserDTO.setLoginUserName(loginUser.getName());
        giftCardUserDTO.setLoginUserPhone(loginUser.getPhone());
        giftCardUserDTO.setAccountId(accountId);
        giftCardUserDTO.setLoginUserPlatform(OperationPlatform.MAINTAIN.code);
        com.tbit.common.entity.view.Result<String> res = giftCardUserApi.rechargeGiftMoney(giftCardUserDTO);
        return res.getCode() == 200 ? Result.success(res.getData()) : Result.errorMessage(res.getMsg(), res.getCode(), null);
    }

    /**
     * 获取用户修改礼品卡金额推荐区域
     *
     * @param userId 用户id
     */
    @GetMapping("/getRecommendArea")
    public Result<List<UserGiftCardAreaVO>> getUserGiftCardRecommendArea(@NotNull Integer userId) {
        List<UserGiftCardAreaVO> list = giftCardUserApi.getUserGiftCardRecommendArea(userId);
        return Result.success(list);
    }
}
